<?php

namespace app\api\controller;

use app\admin\model\shopro\data\Area;
use app\common\controller\Api;
use app\common\model\Prison as PrisonModel;
use think\Db;
use think\Log;
use addons\shopro\job\BaseJob;
use think\queue\Job;
use think\exception\HttpResponseException;
use app\admin\model\shopro\trade\Order;
use app\admin\model\shopro\user\User;
use addons\shopro\service\Wallet as WalletService;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function test()
    {
        //         try {
        //     $order = Order::where('id', 16)->find();
        //     $user = User::get($order['user_id']);
        //     Log::error('test--交易订单支付完成2：' . json_encode([
        //             'order' => $order,
        //             'user' => $user,
        //         ]));
        //     // 数据库删订单的问题常见，这里被删的订单直接把队列移除
        //     if ($order) {
        //         Db::transaction(function () use ($order, $user) {
        //             if ($order->type == 'recharge') {
        //                 // 充值
        //                 $ext = $order->ext;
        //                 $rule = $ext['rule'] ?? [];
        //                 $money = (isset($rule['money']) && $rule['money'] > 0) ? $rule['money'] : 0;
        //                 $gift_type = $rule['gift_type'] ?? 'money';
        //                 $gift = (isset($rule['gift']) && $rule['gift'] > 0) ? $rule['gift'] : 0;
        //                 Log::error('test--交易订单支付完成3：' . json_encode([
        //                         'ext' => $ext,
        //                         'rule' => $rule,
        //                         'money' => $money,
        //                         'gift_type' => $gift_type,
        //                         'gift' => $gift,
        //                     ]));
        //                 if ($money > 0) {
        //                     // 增加余额
        //                     WalletService::change($user, 'money', $money, 'order_recharge', [
        //                         'order_id' => $order->id,
        //                         'order_sn' => $order->order_sn,
        //                         'order_type' => 'trade_order',
        //                     ]);
        //                 }

        //                 if ($gift > 0) {
        //                     // 充值赠送
        //                     WalletService::change($user, $gift_type, $gift, 'recharge_gift', [
        //                         'order_id' => $order->id,
        //                         'order_sn' => $order->order_sn,
        //                         'order_type' => 'trade_order',
        //                     ]);
        //                 }
        //             }
        //         });
        //     }

        // } catch (HttpResponseException $e) {
        //     $data = $e->getResponse()->getData();
        //     $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
        //     format_log_error($e, 'test--OrderPaid.paid.HttpResponseException', $message);
        // } catch (\Exception $e) {
        //     format_log_error($e, 'test--OrderPaid.paid');
        // }
        exit;
//        $list = PrisonModel::where(['tel' => 0])->select();
////        var_dump($list);exit;
//        foreach ($list as $vvv) {
//            PrisonModel::update([
//                'tel'=>'暂无',
//            ],['id'=>$vvv['id']]);
//        }
//        exit;

//        $list = PrisonModel::fieldRaw('DISTINCT province_id,city_id,p_name,c_name,a_name')->where(['area_id' => 0])->select();
//        foreach ($list as $vvv) {
//            $districtRes = Area::create([
//                'pid' => $vvv['city_id'],
//                'name' => $vvv['a_name'],
//                'level' => 'district',
//            ]);
//            PrisonModel::update([
//                'area_id' => $districtRes->id
//            ], ['a_name' => $vvv['a_name'], 'province_id' => $vvv['province_id'], 'city_id' => $vvv['city_id']]);
////            exit;
//        }
        exit;
        $list = PrisonModel::where('lng', '=', '')->select();
//        var_dump($list);exit;
        foreach ($list as $item) {
            $item['c_name'] = trim($item['c_name']);
            $item['a_name'] = trim($item['a_name']);
            $item['address'] = trim($item['address']);
            $result = getLngLatByAddress($item['p_name'] . $item['c_name'] . $item['a_name'] . $item['address']);
//            var_dump($result);exit;
            PrisonModel::update([
                'c_name' => $item['c_name'],
                'a_name' => $item['a_name'],
                'address' => $item['address'],
                'lng' => $result['lng'],
                'lat' => $result['lat'],
            ], ['id' => $item['id']]);
        }
        exit;
        // 0看守所  1监狱
        $type = 1;
        $url = 'https://www.shuqianfan.com/api/zero.index/dizhiAndType?page=%s&type=' . $type;
        $arr = [];
        for ($i = 40; $i <= 61; $i++) {
            $res = file_get_contents(sprintf($url, $i));
            $res = json_decode($res, true);
//            var_dump($i, $res);
            $s1 = '市';
            $s2 = '县';
            $s3 = '区';
            $s4 = '镇';
            $s5 = '街道';
            $f1 = false;
            foreach ($res['data'] as $re) {
                if (!in_array($re['chengshi'], $arr)) {
                    if ($re['address'] == '凤凰大道688号') {
                        $re['address'] = '临邛镇' . $re['address'];
                    }
                    if ($re['title'] == '雅安市看守所') {
                        $re['address'] = '名山区新民南路16号';
                    }
                    if ($re['title'] == '眉山市看守所') {
                        $re['address'] = '东坡区X04世纪幼儿园东北侧';
                    }
                    if ($re['title'] == '岱山县看守所') {
                        $re['address'] = '岱山县' . $re['address'];
                    }
                    if ($re['title'] == '嵊泗县看守所') {
                        $re['address'] = '嵊泗县' . $re['address'];
                    }
                    if ($re['title'] == '广州市第一看守所') {
                        $re['address'] = '白云区' . $re['address'];
                    }
                    if ($re['title'] == '东莞市第二看守所') {
                        $re['address'] = '东城街道' . $re['address'];
                    }
                    if ($re['title'] == '长宁区看守所') {
                        $re['address'] = '长宁区' . $re['address'];
                    }
                    if ($re['title'] == '普陀区看守所') {
                        $re['address'] = '普陀区' . $re['address'];
                    }
                    if ($re['title'] == '新沂市看守所') {
                        $re['address'] = '新沂市' . $re['address'];
                    }
                    if ($re['title'] == '梁河县看守所') {
                        $re['address'] = '梁河县' . $re['address'];
                    }
                    if ($re['title'] == '阜阳监狱') {
                        $re['address'] = '颍东区' . $re['address'];
                    }
                    if ($re['title'] == '安徽省安庆监狱') {
                        $re['address'] = '迎江区' . $re['address'];
                    }
                    if ($re['title'] == '蜀山监狱') {
                        $re['address'] = '蜀山区' . $re['address'];
                    }
                    if ($re['title'] == '安徽省未成年犯管教所（省庐州女子监狱）') {
                        $re['address'] = '蜀山区' . $re['address'];
                    }
                    if ($re['title'] == '金昌监狱') {
                        $re['address'] = '金川区' . $re['address'];
                    }
                    if ($re['title'] == '天祝监狱') {
                        $re['address'] = '天祝县' . $re['address'];
                    }
                    if ($re['title'] == '临夏监狱') {
                        $re['address'] = '临夏市' . $re['address'];
                    }
                    if ($re['title'] == '清远监狱') {
                        $re['address'] = '清城区' . $re['address'];
                    }
                    if ($re['title'] == '梅州监狱') {
                        $re['address'] = '梅江区' . $re['address'];
                    }
                    if ($re['title'] == '新康监狱') {
                        $re['address'] = '琼山区' . $re['address'];
                    }
                    if ($re['title'] == '衡水监狱') {
                        $re['address'] = '桃城区' . $re['address'];
                    }
                    if ($re['title'] == '佳木斯监狱') {
                        $re['address'] = '莲江口镇' . $re['address'];
                    }
                    if ($re['title'] == '河南省监狱管理局') {
                        $re['address'] = '金水区' . $re['address'];
                    }
                    if ($re['title'] == '河南省第一监狱') {
                        $re['address'] = '金明区' . $re['address'];
                    }
                    if ($re['title'] == '河南省第二监狱') {
                        $re['address'] = '牧野区' . $re['address'];
                    }
                    if ($re['title'] == '河南省女子监狱') {
                        $re['address'] = '卫滨区' . $re['address'];
                    }
                    if ($re['title'] == '河南省豫北监狱') {
                        $re['address'] = '牧野区' . $re['address'];
                    }
                    if ($re['title'] == '河南省豫西监狱') {
                        $re['address'] = '瀍河回族区' . $re['address'];
                    }
                    if ($re['title'] == '河南省豫东监狱') {
                        $re['address'] = '睢阳区' . $re['address'];
                    }
                    if ($re['title'] == '河南省焦作未管所') {
                        $re['address'] = '解放区' . $re['address'];
                    }
                    if ($re['title'] == '商丘市监狱') {
                        $re['address'] = '梁园区' . $re['address'];
                    }
                    if ($re['title'] == '驻马店市监狱') {
                        $re['address'] = '驿城区' . $re['address'];
                    }
                    if ($re['title'] == '湖北省沙洋广华监狱') {
                        $re['address'] = '广华镇' . $re['address'];
                    }
                    if ($re['title'] == '湖北省沙洋熊望台监狱') {
                        $re['address'] = '多宝镇' . $re['address'];
                    }
                    if ($re['title'] == '湖北省荆州监狱') {
                        $re['address'] = '荆州区' . $re['address'];
                    }
                    if ($re['title'] == '恩施监狱') {
                        $re['address'] = '恩施市' . $re['address'];
                    }
                    if ($re['title'] == '汉江监狱') {
                        $re['address'] = '干河街道' . $re['address'];
                    }
                    if ($re['title'] == '湖北省咸宁监狱') {
                        $re['address'] = '咸安区' . $re['address'];
                    }
                    if ($re['title'] == '湖南省郴州监狱') {
                        $re['address'] = '北湖区' . $re['address'];
                    }
                    if ($re['title'] == '镇江监狱') {
                        $re['address'] = '京口区' . $re['address'];
                    }
                    if ($re['title'] == '江苏省南通监狱') {
                        $re['address'] = '崇川区' . $re['address'];
                    }
                    if ($re['title'] == '江苏省南通女子监狱') {
                        $re['address'] = '崇川区' . $re['address'];
                    }
                    if ($re['title'] == '乌兰监狱') {
                        $re['address'] = '音德尔镇' . $re['address'];
                    }
                    if ($re['title'] == '乌塔其监狱') {
                        $re['address'] = '音德尔镇' . $re['address'];
                    }
                    if ($re['title'] == '保安沼监狱') {
                        $re['address'] = '音德尔镇' . $re['address'];
                    }
                    if ($re['title'] == '西川监狱') {
                        $re['address'] = '城西区' . $re['address'];
                    }
                    if ($re['title'] == '长宁监狱') {
                        $re['address'] = '大通县' . $re['address'];
                    }
                    if ($re['title'] == '济宁监狱') {
                        $re['address'] = '高新区长虹路16号';
                    }
                    if ($re['title'] == '山西省未成年管教所') {
                        $re['address'] = '万柏林区' . $re['address'];
                    }
                    if ($re['title'] == '商州监狱') {
                        $re['address'] = '商州区' . $re['address'];
                    }
                    if ($re['title'] == '陕西省女子监狱') {
                        $re['address'] = '未央区' . $re['address'];
                    }
                    if ($re['title'] == '曲江监狱') {
                        $re['address'] = '雁塔区' . $re['address'];
                    }
                    if ($re['title'] == '浙江省女子监狱') {
                        $re['address'] = '西湖区' . $re['address'];
                    }
                    if ($re['title'] == '宁波市望春监狱') {
                        $re['address'] = '海曙区' . $re['address'];
                    }
                    if ($re['title'] == '京山线茶淀站 103') {
                        $re['address'] = '汉沽区' . $re['address'];
                    }
                    if ($re['title'] == '京山线茶淀站112信箱') {
                        $re['address'] = '汉沽区' . $re['address'];
                    }
                    $re['area'] = '';
                    $re['new_address'] = '';
                    $a1 = explode($s1, $re['address'], 2);
                    $is_get = 0;
                    if (count($a1) == 2) {
//                        echo $s1 . ' ===== ' . $re['address'] . PHP_EOL;
                        $re['area'] = $a1[0] . $s1;
                        $re['new_address'] = $a1[1];
                        $is_get = 1;
                    }
                    if (!$is_get) {
                        $a2 = explode($s2, $re['address'], 2);
                        if (count($a2) == 2) {
//                        echo $s2 . ' ===== ' . $re['address'] . PHP_EOL;
                            $re['area'] = $a2[0] . $s2;
                            $re['new_address'] = $a2[1];
                            $is_get = 1;
                        }
                    }
                    if (!$is_get) {
                        $a3 = explode($s3, $re['address'], 2);
                        if (count($a3) == 2) {
//                        echo $s3 . ' ===== ' . $re['address'] . PHP_EOL;
                            $re['area'] = $a3[0] . $s3;
                            $re['new_address'] = $a3[1];
                            $is_get = 1;
                        }
                    }
                    if (!$is_get) {
                        $a4 = explode($s4, $re['address'], 2);
                        if (count($a4) == 2) {
//                        echo $s4 . ' ===== ' . $re['address'] . PHP_EOL;
                            $re['area'] = $a4[0] . $s4;
                            $re['new_address'] = $a4[1];
                            $is_get = 1;
                        }
                    }
                    if (!$is_get) {
                        $a5 = explode($s5, $re['address'], 2);
                        if (count($a5) == 2) {
//                        echo $s5 . ' ===== ' . $re['address'] . PHP_EOL;
                            $re['area'] = $a5[0] . $s5;
                            $re['new_address'] = $a5[1];
                        }
                    }
                    if (!$re['area']) {
                        echo $re['title'] . ' 区域出现异常，请检查！' . PHP_EOL;
                        var_dump($re);
                        exit;
                    }
                    if (!$re['address']) {
                        echo $re['title'] . ' 地址出现异常，请检查！' . PHP_EOL;
                        var_dump($re);
                        exit;
                    }
                    $chengshi = explode('/', $re['chengshi']);
                    $chengshi[0] = trim($chengshi[0]);
                    $chengshi[1] = trim($chengshi[1]);
                    $province_id = Area::whereLike('name', '%' . $chengshi[0] . '%')->value('id') ?: 0;
                    $city_id = Area::whereLike('name', '%' . $chengshi[1] . '%')->value('id') ?: 0;
                    $area_id = Area::whereLike('name', '%' . $re['area'] . '%')->value('id') ?: 0;
                    $info = PrisonModel::get(['name' => $re['title'], 'type' => $type]);
                    if (!$info) {
                        $data = [
                            'name' => $re['title'],
                            'p_name' => $chengshi[0],
                            'province_id' => $province_id,
                            'c_name' => $chengshi[1],
                            'city_id' => $city_id,
                            'a_name' => $re['area'],
                            'area_id' => $area_id,
                            'address' => $re['new_address'] ?: $re['address'],
                            'o_address' => $re['address'],
                            'tel' => $re['phone'],
                            'type' => $type,
                        ];
//                        var_dump($data);
//                        exit;
                        $data['a_name'] = trim($data['a_name']);
                        $data['address'] = trim($data['address']);
                        $result = getLngLatByAddress($data['p_name'] . $data['c_name'] . $data['a_name'] . $data['address']);
                        $data['lng'] = $result['lng'];
                        $data['lat'] = $result['lat'];
                        PrisonModel::create($data);
                    }
                }
            }
        }
        $this->success('返回成功', $this->request->param());
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
